package com.example2016.cjj.wxplangamedemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example2016.cjj.wxplangamedemo.R;
import com.example2016.cjj.wxplangamedemo.game2.AutoSprite;
import com.example2016.cjj.wxplangamedemo.game2.BattlePlane;
import com.example2016.cjj.wxplangamedemo.game2.BigEnemyPlane;
import com.example2016.cjj.wxplangamedemo.game2.BombAward;
import com.example2016.cjj.wxplangamedemo.game2.Bullet;
import com.example2016.cjj.wxplangamedemo.game2.BulletAward;
import com.example2016.cjj.wxplangamedemo.game2.EnemyPlane;
import com.example2016.cjj.wxplangamedemo.game2.MiddleEnemyPlane;
import com.example2016.cjj.wxplangamedemo.game2.SmallEnemyPlane;
import com.example2016.cjj.wxplangamedemo.game2.Sprite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.example2016.cjj.wxplangamedemo.R.mipmap.bg;

/**
 * Created by CJJ on 2017/4/1.
 */

public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{
    private static final String TAG = "GameSurfaceView";

    private int gameRefeshTime = 20;//游戏刷新时间为50毫秒一次

    private SurfaceHolder mHolder;
    //private Canvas canvas;
    private boolean mIsRunning = false;

    public static  final  int STATUS_GAME_STARTED =1;  //游戏开始状态
    public static  final  int STATUS_GAME_PAUSED =2;  //游戏暂停状态
    public static  final  int STATUS_GAME_OVER =3;   //游戏结束状态
    public static  final  int STATUS_GAME_DESTROYED =4;  //游戏销毁状态

    private int currentStatus = STATUS_GAME_DESTROYED;  //初始为销毁状态


    private static final int TOUCH_TYPE_MOVE=5;  //触摸类型：移动
    private static final int TOUCH_TYPE_SINGLECLICK = 6; //触摸类型：单击
    private static final int TOUCH_TYPE_DOUBLECLICK = 7; //触摸类型：双击
    private static final int TAP = 8; //手指按下了
    private static final int GAME_OVER=9;//用于发送游戏结束msg标识

    private boolean mStillDown = false; //手指按下标识
    private boolean mSingleTap = false; //一次按下标识

    //一次单击事件由DOWN和UP两个事件合成，假设从down到up间隔小于200毫秒，我们就认为发生了一次单击事件
    private int singleClickDuration = 200;
    //一次双击事件由两个点击事件合成，两个单击事件之间小于300毫秒，我们就认为发生了一次双击事件
    private int doubleClickDuration = 300;

    private float downX=-1,downY=-1; //手指down事件时的x，y
    private long downTime = -1; //手指按下的时间
    private long upTime = -1;   //手指抬起的时间
    private long lastSingleClickTime = -1;//上一次单击事件的时间
    private float touchX=-1,touchY=-1; //手指在屏幕上的x，y，主要用于移动战斗机
    private boolean isOnBattlePlan = false; //移动事件下，判断手指按下的点是否在战斗机范围内标识


    private Bitmap planbitmap;
    private BattlePlane battlePlane;

    private Paint mPaint;
    private Paint mTextPaint;
    private int mfrontSize = 12;//默认的字体大小，用于绘制左上角的文本

    private long frame = 0;//总共绘制的帧数

    private float density = getResources().getDisplayMetrics().density; //屏幕密度

    private Bitmap pause1Bitmap; //暂停1图
    private Bitmap pause2Bitmap; //暂停2图
    private Bitmap smallEnemyPlanBitmap; //小敌机图
    private Bitmap middleEnemyPlanBitmap; //中敌机图
    private Bitmap bigEnemyPlanBitmap; //大敌机图
    private Bitmap bulletBitmap; //子弹图
    private Bitmap explodeBitmap; //爆炸效果图bitmap
    private Bitmap bombAwardBitmap; //炸弹奖励道具
    private Bitmap bulletAwardBitmap; //双发子弹道具
    private Bitmap bombBitmap; //炸弹图
    private Bitmap bgBitmap; //背景图

    //屏幕上所有的Sprites
    private List<Sprite> allSprites = new ArrayList<>();

    //需要添加的Sprites
    private List<Sprite> spritesNeedAdded = new ArrayList<>();

    //总得分
    private long score = 0;


    private RectF continueBtnRectF; // 继续、重新开始按钮的Rectf

    private Rect bgRect;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case  TAP:
                    if( !mStillDown ){
                        onSingleClick(downX,downY);
                    }else {
                        mSingleTap = true;
                    }
                    break;
                case GAME_OVER:
                    currentStatus = STATUS_GAME_OVER;
                    break;
            }
        }
    };



    public GameSurfaceView(Context context) {
        this(context,null);
    }

    public GameSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GameSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }


    private void init() {

        mHolder = getHolder();
        mHolder.addCallback(this);



        //获取图片
        planbitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.plane);
        explodeBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.explosion);
        bulletBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.yellow_bullet);
        smallEnemyPlanBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.small);
        middleEnemyPlanBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.middle);
        bigEnemyPlanBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.big);
        pause1Bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.pause1);
        pause2Bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.pause2);
        bombAwardBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.bomb_award);
        bulletAwardBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.bullet_award);
        bombBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.bomb);
        bgBitmap = BitmapFactory.decodeResource(getResources(), bg);

        bgRect = new Rect();
        bgRect.left = 0;
        bgRect.top = 0;
        bgRect.right =bgBitmap.getWidth();
        bgRect.bottom = bgBitmap.getHeight();



        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
        mTextPaint.setColor(0xff000000);

        startWhenReady();

    }


    private void  startWhenReady(){
        battlePlane = new BattlePlane(planbitmap);
        //将游戏设置为开始状态
        currentStatus = STATUS_GAME_STARTED;

        postInvalidate();
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsRunning = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsRunning = false;
    }


    @Override
    public void run() {
        while (mIsRunning){
            long start = System.currentTimeMillis();

            draw();

            long end = System.currentTimeMillis();
            //通过这两个时间戳的差值，就可以知道这些函数所消耗的时间：
            // 如果(end - start) > X, 那线程就完全没有必要去休眠；
            // 如果(end - start) < X， 那线程的休眠时间应该为 X - (end - start) 。
            try {
                if( end - start < gameRefeshTime ) {
                    Thread.sleep(gameRefeshTime - (end - start));
                }
            } catch (InterruptedException e) {
                    e.printStackTrace();
            }

        }
    }

    Canvas canvas;

    private void draw() {
        canvas = mHolder.lockCanvas();
        try {
              if( canvas !=null ) {
                  //画背景
                  RectF bgDstRectF = new RectF();
                  bgDstRectF.left =0;
                  bgDstRectF.top=0;
                  bgDstRectF.right = canvas.getWidth();
                  bgDstRectF.bottom = canvas.getHeight();
                  canvas.drawBitmap(bgBitmap,bgRect,bgDstRectF,mPaint);

                    switch (currentStatus) {
                        case STATUS_GAME_STARTED:
                            drawGameStated(canvas);
                            break;
                        case STATUS_GAME_PAUSED:
                            drawGamePaused(canvas);
                            break;
                        case STATUS_GAME_OVER:
                            drawGameOver(canvas);
                            break;
                    }
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }finally {
                mHolder.unlockCanvasAndPost(canvas);
            }

    }






    //绘制游戏开始画面
    private void drawGameStated(Canvas canvas) {
        //绘制左上角的得分数和左下角的炸弹数
        drawScoreAndBombs(canvas);

        //第一次绘制时，将战斗机移到Canvas最下方，在水平方向的中心
        if( frame ==0 ){
            float centerX = getWidth()/2 - battlePlane.getWidth()/2;
            float centerY =getHeight() - battlePlane.getHeight();
            battlePlane.moveTo(centerX,centerY);
        }

        //每隔30帧，随机添加敌机、奖励Sprite
        if( frame%30 == 0 ){
            createRandomSprite(canvas.getWidth());
        }



        //将spritesNeedAdded添加到allSprites中
        if( spritesNeedAdded.size() > 0 ){
            allSprites.addAll(spritesNeedAdded);
            spritesNeedAdded.clear();
        }


        //在绘制之前，从allSprites中移除掉所有被destroy的Sprite
        removeDestroySprites();

        //遍历allSprites，绘制敌机、子弹、奖励、爆炸效果
        Iterator<Sprite> iterator = allSprites.iterator();
        while ( iterator.hasNext() ){
            Sprite sprite = iterator.next();
            if( !sprite.isDestroy() ){
                //在Sprite的draw方法内有可能会调用destroy方法
                sprite.draw(canvas,mPaint,this);
            }

            //我们此处要判断Sprite在执行了draw方法后是否被destroy掉了
            if( sprite.isDestroy() ){
                //如果destroy了，那么从allSprites中移除
                iterator.remove();
            }
        }


        //绘制战斗机
        if( battlePlane !=null ){
            battlePlane.draw(canvas,mPaint,this);
            if( battlePlane.isDestroy() ){
                //战斗机被销毁，那么游戏结束
                // currentStatus = STATUS_GAME_OVER;

                //使用handler延迟1s,把状态改为游戏结束，这样能够让战斗机的爆炸动画播放完。
                mHandler.sendEmptyMessageDelayed(GAME_OVER,1000);
            }
        }


        frame ++;

        //通过调用invalidate()方法使得View持续渲染，实现动态效果
       // invalidate();
    }



    //绘制游戏暂停画面
    private void drawGamePaused(Canvas canvas) {
        //绘制左上角的得分数和左下角的炸弹数
        drawScoreAndBombs(canvas);

        //调用Sprite的onDraw方法，而非draw方法，
        // 这样就能渲染静态的Sprite，而不让Sprite改变位置
        for( Sprite sprite : allSprites){
            sprite.onDraw(canvas,mPaint,this);
        }

        if( battlePlane !=null ){
            battlePlane.draw(canvas,mPaint,this);
        }

        drawScoreDialog(canvas,"继续");

    }


    //绘制游戏结束画面
    private void drawGameOver(Canvas canvas) {
        drawScoreDialog(canvas,"重新开始");
    }


    //绘制得分对话框
    private void drawScoreDialog(Canvas canvas,String btnStr) {
        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();

        int rectWidth = (int) (300*density);
        int rectHeight = (int) (200*density);
        RectF rectF = new RectF();
        rectF.left = canvas.getWidth()/2- rectWidth/2;
        rectF.top = canvas.getHeight()/5;
        rectF.right = rectF.left + rectWidth;
        rectF.bottom = rectF.top + rectHeight;

        //绘制背景
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(0xCFD7DDDE);
        canvas.drawRect(rectF,mPaint);

        //画边框
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4);
        mPaint.setColor(Color.BLACK);
        canvas.drawRect(rectF,mPaint);

        //绘制文本"飞机大战分数"
        mTextPaint.setTextSize( 20*density );
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        int textX = (int) (canvas.getWidth()/2);
        int textY = (int) (rectF.top + 30*density);
        canvas.drawText("飞机大战分数",textX,textY,mTextPaint);

        //绘制文本下的直线
        canvas.save();
        canvas.translate(0, textY+ 10*density );
        canvas.drawLine( rectF.left,0, rectF.right,0,mPaint);

        //绘制得分
        int scoreX =(int) (canvas.getWidth()/2);
        int scoreY = (int) ( 70*density );
        canvas.drawText( String.valueOf(getScore())  ,scoreX,scoreY,mTextPaint);


        //绘制得分下的直线
        canvas.translate(0, rectHeight -85*density);
        canvas.drawLine( rectF.left,0, rectF.right,0,mPaint);

        //绘制继续按钮与边框
        int contX=(int) (canvas.getWidth()/2);
        int contY= (int) (30*density);
        canvas.drawText(btnStr,contX,contY,mTextPaint);

        int contWidth = (int) (100 * density);
        int contHeight = (int) (30 * density);
        RectF contRectf = new RectF();
        contRectf.left = canvas.getWidth()/2 - contWidth/2;
        contRectf.top =  8*density;
        contRectf.right = contRectf.left + contWidth;
        contRectf.bottom = contRectf.top + contHeight;
        canvas.drawRect(contRectf,mPaint);
        canvas.restore();

        continueBtnRectF = new RectF();
        continueBtnRectF.left = contRectf.left;
        continueBtnRectF.top = contRectf.top + rectHeight -85*density + textY+ 10*density ;
        continueBtnRectF.right = contRectf.right;
        continueBtnRectF.bottom =  continueBtnRectF.top + contHeight;

    }





    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int touchType = resolveTouchType(event);

        if( currentStatus == STATUS_GAME_STARTED ){
            if( touchType == TOUCH_TYPE_MOVE ){
                onMove(touchX,touchY);
            }else if( touchType == TOUCH_TYPE_DOUBLECLICK ){
                //Log.i(TAG, "onTouchEvent: 双击放炸弹");
                useBomb();
            }else  if ( touchType == TOUCH_TYPE_SINGLECLICK){
                onSingleClick(downX,downY);
            }
        }


        return true;
    }


    //使用炸弹
    private void useBomb() {
        battlePlane.useBomb(this);
    }


    //解析触摸事件，来合成我们想要的事件类型
    private int resolveTouchType(MotionEvent event) {
        int touchtype = -1;

        touchX = event.getX();
        touchY = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();

                downTime = System.currentTimeMillis();

                //延迟处理单击事件，
                // 手指按下时，通过mHandler延迟doubleClickDuration的时间去发送一个TAP消息，
                // 如果在doubleClickDuration的时间中没有再次按下，
                // 那mhandler在handleMessage中收到了TAP消息，就说明是单击事件,在mhandler中处理。否则就是双击事件
                mStillDown = true;
                boolean hadTapMessage = mHandler.hasMessages(TAP);
                if( hadTapMessage ){
                    mHandler.removeMessages(TAP);
                }
                else
                {
                    mHandler.sendEmptyMessageDelayed(TAP,doubleClickDuration);
                }


                break;
            case MotionEvent.ACTION_MOVE:
                long deltaTime = System.currentTimeMillis() - downTime;
                if( deltaTime > singleClickDuration ){
                    touchtype = TOUCH_TYPE_MOVE;


                    mHandler.removeMessages(TAP);
                }
                break;
            case MotionEvent.ACTION_UP:
                mStillDown = false;

                isOnBattlePlan = false;

                upTime = System.currentTimeMillis();
                long downUpDuration = upTime - downTime;    //计算触点按下到触点弹起之间的时间差

                //如果此次触点按下和抬起之间的时间差小于一次单击事件指定的时间差，
                //那么我们就认为发生了一次单击
                if( downUpDuration <= singleClickDuration){
                    if( (upTime - lastSingleClickTime) <=doubleClickDuration ){
                        //如果两次单击的时间差小于一次双击事件执行的时间差，
                        //那么我们就认为发生了一次双击事件
                        touchtype = TOUCH_TYPE_DOUBLECLICK;
                        //重置变量
                        lastSingleClickTime = -1;
                        downTime =-1;
                        upTime = -1;

                        mHandler.removeMessages(TAP);
                    }else {

                        if( mSingleTap){
                            touchtype = TOUCH_TYPE_SINGLECLICK;
                            mSingleTap = false;
                        }

                        lastSingleClickTime = upTime;
                    }


                }

                break;
        }
        return touchtype;
    }




    //单击执行事件内容
    private void onSingleClick(float x,float y){
        if( currentStatus == STATUS_GAME_STARTED){
            if( isClickPause(x,y)){
                //Log.i(TAG, "onSingleClick: 暂停游戏");
                gamePause();

            }
        }else  if( currentStatus== STATUS_GAME_PAUSED ){
            if( isClickContinue(x,y)){
                //Log.i(TAG, "onSingleClick: jixu");
                gameResume();
            }
        }else  if( currentStatus== STATUS_GAME_OVER ){
            if( isClickContinue(x,y)){
                gameRestart();
            }
        }
    }




    //暂停游戏
    private void gamePause() {
        currentStatus = STATUS_GAME_PAUSED;
    }


    //继续游戏
    private void gameResume() {
        currentStatus = STATUS_GAME_STARTED;
        postInvalidate();
    }

    //重新开始游戏
    private void gameRestart() {

        //将游戏设置为销毁状态
        currentStatus = STATUS_GAME_DESTROYED;
        //重置frame
        frame = 0;

        //重置得分
        score =0;

        //销毁战斗机
        if( battlePlane!=null ){
            battlePlane.destroy();
        }
        battlePlane = null;

        //销毁敌机、子弹、奖励、爆炸
        for(Sprite sprite : allSprites ){
            sprite.destroy();
        }
        allSprites.clear();
        spritesNeedAdded.clear();


        //重新开始
        startWhenReady();

    }


    //移动事件内容
    private void onMove(float x, float y) {
        //如果按下的点在战斗机的范围内，那就移动战斗机，否则不处理。
        if( battlePlane != null){

            if( !isOnBattlePlan){
                RectF battlePlanRectf = battlePlane.getRectf();
                if( battlePlanRectf.contains(downX,downY) ){
                    isOnBattlePlan = true;
                }
            }else {
                battlePlane.centerTo(x,y);
            }


        }
    }




    //判断x,y是否在暂停图片的RectF内
    //如果是，那就说明是点击了暂停图片。
    private boolean isClickPause(float x,float y){
        return getPauseBitmapDstRectF().contains(x,y);
    }


    //判断x,y是否在继续的RectF内
    private boolean isClickContinue(float x,float y){
        return continueBtnRectF.contains(x,y);
    }


    //绘制左上角的得分数和左下角的炸弹数
    private void drawScoreAndBombs(Canvas canvas) {
        //绘制暂停图片
        Bitmap pauseBitmap = currentStatus==STATUS_GAME_STARTED ? pause1Bitmap : pause2Bitmap;
        RectF pauseRectF = getPauseBitmapDstRectF();
        canvas.drawBitmap(pauseBitmap,pauseRectF.left,pauseRectF.top,mPaint);

        //绘制总得分
        float x = pauseRectF.right + 10*density;
        float y = pauseRectF.top + 15*density;
        mTextPaint.setTextSize( mfrontSize*density );
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText("Score:"+score,x,y,mTextPaint);


        //绘制炸弹图片
        RectF bombRect = getBombBitmapDstRectF();
        canvas.drawBitmap(bombBitmap,bombRect.left,bombRect.top,mPaint);

        //绘制炸弹数量
        int bombcount =0;
        if( battlePlane!=null){
            bombcount = battlePlane.getBombCount();
        }
        float x2 = bombRect.right + 10*density;
        float y2 = bombRect.top + 18*density;
        mTextPaint.setTextSize( mfrontSize*density );
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText("X"+bombcount,x2,y2,mTextPaint);

    }


    //随机添加敌机、奖励Sprite
    private void createRandomSprite(int canvasWidth) {
        Sprite sprite =null;
        int speed = 2;

        int callCounts = Math.round( frame/30); //是createRandomSprite方法的调用次数
        if( (callCounts+1)%25 == 0 ){
            //发送道具奖品
            if( (callCounts+1)%50 == 0 ){
                //发送炸弹道具
                sprite = new BombAward(bombAwardBitmap);
            }else {
                //发送双子弹道具
                sprite = new BulletAward(bulletAwardBitmap);
            }

        }else{
            //随机绘制敌机
            int[] nums = {0,0,0,0,1,0,0,1,1,2};
            int index = (int) Math.floor( nums.length * Math.random() );
            int type = nums[index];
            switch (type){
                case 0:
                    //小敌机
                    sprite = new SmallEnemyPlane(smallEnemyPlanBitmap);
                    break;
                case 1:
                    //中敌机
                    sprite = new MiddleEnemyPlane(middleEnemyPlanBitmap);
                    break;
                case 2:
                    //大敌机
                    sprite = new BigEnemyPlane(bigEnemyPlanBitmap);
                    break;
            }

            //随机设置下落速度
            if( type !=2 ){
                if( Math.random() > 0.33 ){
                    speed = 4;
                }
            }
        }



        if( sprite!=null ){
            float x = (float) ((canvasWidth-sprite.getWidth())*Math.random()); //x轴随机生成
            float y = - sprite.getHeight(); //y值为负数，在屏幕外
            sprite.setX(x);
            sprite.setY(y);
            if( sprite instanceof AutoSprite){
                ((AutoSprite) sprite).setSpeed(speed); //设置speed
            }

            addNeededSprite(sprite);
        }

    }


    //获取屏幕密度
    public float getDensity(){
        return  density;
    }


    //获取处于活动状态的子弹
    public List<Bullet> getAliveBullets(){
        List<Bullet> bullets = new ArrayList<>();
        for (Sprite sprite : allSprites ){
            if( !sprite.isDestroy() && sprite instanceof Bullet ){
                bullets.add( (Bullet) sprite );
            }
        }
        return bullets;
    }

    //获取处于活动状态的敌机
    public List<EnemyPlane> getAliveEnemys(){
        List<EnemyPlane> enemyPlanes = new ArrayList<>();
        for (Sprite sprite : allSprites){
            if( !sprite.isDestroy() && sprite instanceof EnemyPlane ){
                enemyPlanes.add( (EnemyPlane) sprite );
            }
        }

        return enemyPlanes;
    }



    //获取处于活动状态的双发子弹奖励
    public List<BulletAward> getAliveBulletAward(){
        List<BulletAward> bulletAwards = new ArrayList<>();
        for (Sprite sprite : allSprites){
            if( !sprite.isDestroy() && sprite instanceof BulletAward ){
                bulletAwards.add( (BulletAward) sprite );
            }
        }
        return bulletAwards;
    }


    //获取处于活动状态的双发子弹奖励
    public List<BombAward> getAliveBombAward(){
        List<BombAward> bombAwards = new ArrayList<>();
        for (Sprite sprite : allSprites){
            if( !sprite.isDestroy() && sprite instanceof BombAward ){
                bombAwards.add( (BombAward) sprite );
            }
        }
        return bombAwards;
    }




    //获取爆炸效果图
    public Bitmap getExplosinBitmap(){
        return explodeBitmap;
    }

    //获取子弹图
    public Bitmap getBulletBitmap(){
        return bulletBitmap;
    }


    //向spritesNeedAdded中添加Sprite
    public  void  addNeededSprite(Sprite sprite){
        spritesNeedAdded.add(sprite);
    }

    //添加总得分
    public void  addScore(int value){
        this.score +=value;
    }

    //获取总得分
    public long getScore(){
        return this.score;
    }

    //从allSprites中移除掉所有被destroy的Sprite
    private void removeDestroySprites() {
        Iterator<Sprite> iterator = allSprites.iterator();
        while ( iterator.hasNext() ){
            Sprite sprite = iterator.next();
            if(sprite.isDestroy()){
                iterator.remove();
            }
        }
    }


    //获取暂停图片的RectF
    private RectF getPauseBitmapDstRectF(){
        Bitmap pauseBitmap = currentStatus==STATUS_GAME_STARTED ? pause1Bitmap : pause2Bitmap;
        RectF rectF = new RectF();
        rectF.left = 15 * density;
        rectF.top = 15 * density;
        rectF.right = rectF.left + pauseBitmap.getWidth();
        rectF.bottom = rectF.top + pauseBitmap.getHeight();

        return rectF;
    }

    //获取炸弹图片的RectF
    private RectF getBombBitmapDstRectF(){
        RectF rectF = new RectF();
        rectF.left = 15 * density;
        rectF.top =  (getMeasuredHeight()-bombBitmap.getHeight()) - 15 * density;
        rectF.right = rectF.left + bombBitmap.getWidth();
        rectF.bottom = rectF.top + bombBitmap.getHeight();

        return rectF;
    }


}
